<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_AjaxDeleteFolderContentAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$folderId = $this->getRequestParameter("docId");
		$lang = $this->getRequestParameter("docLang");
        $recursive = $this->getRequestParameter("recursive", false);
        $nyroModal = $this->getRequestParameter("nyroModal");
		$rows = $this->getFormValues("row");
		$nbRows = count($rows);
        $errors = array();        

		if(!is_null($folderId))
		{
			$folder = $this->getDocumentProvider()->getByModelAndUniqueId("modules_backoffice/folder", $folderId, $lang);
            $this->addToModel("folder", $folder);
		}

		$uniqueRow = $this->getDocumentProvider()->getByUniqueId($rows[0], $lang);

		if($nyroModal == 1)
		{
			$docProvider = $this->getDocumentProvider();
			foreach($rows as $contentId)
			{
				$content = $docProvider->getByModelAndUniqueId($uniqueRow->getModel(), $contentId, $lang);
				((bool)$recursive && $recursive == "true") ? $content->deleteRecursive() : $content->delete();
			}
			
			$errors["status"] = "success";
		}
		
		$this->addToModel("errors", $errors);
		$this->addToModel("lang", $lang);
		$this->addToModel("rows", $rows);
		$this->addToModel("nbRows", $nbRows);
        $this->addToModel("uniqueRow", $uniqueRow);
        $this->addToModel("recursive", $recursive);

		$this->setView('success');
	}	
}